package com.lanou.service.T8TService.impl;

import com.lanou.bean.ShoppingBean;
import com.lanou.bean.T8Tcommodity.ShoppingCart;
import com.lanou.mapper.ShoppingCartMapper;
import com.lanou.service.T8TService.ShoppingCartService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/19.
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    ShoppingCartMapper shoppingCartMapper;


    //查询购物车
    @Override
    public List<ShoppingCart> selectUserCart(@Param(value = "userId") Integer userId) {
        List<ShoppingBean> shoppingBeanList = shoppingCartMapper.selectUserCart(userId);
        List<ShoppingCart> cartList = new ArrayList<>();
        for (ShoppingBean s : shoppingBeanList) {
            ShoppingCart cart = new ShoppingCart();
            String comName = shoppingCartMapper.getCommodityNameById(s.getCommodity_id());


            String color = shoppingCartMapper.getColorById(s.getCart_id());
            String src = shoppingCartMapper.getSrcById(s.getCart_id());
            String count = shoppingCartMapper.getCountById(s.getCart_id());
            String price = shoppingCartMapper.getPriceById(s.getCommodity_id());

            cart.setCommodityName(comName);
            cart.setCommodityColor(color);
            cart.setCommodityImgSrc(src);
            cart.setCount(count);
            cart.setId(s.getCommodity_id());
            cart.setPrice(price);
            cartList.add(cart);
        }
        return cartList;
    }

    //添加购物车
    @Override
    public void addCart(@Param(value = "commodityId") Integer commodityId, @Param(value = "colorId") Integer colorId, @Param(value = "size") Integer size, @Param(value = "userId") Integer userId, @Param(value = "count") Integer count) {

        //根据commodityID获取img和price
        String img = getImgId(commodityId);
        String price = getPriceID(commodityId);

        //插入数据库
        shoppingCartMapper.addCart(commodityId, colorId, size, userId, count, img, price);
    }

    //根据commodityID获取priceId
    private String getPriceID(Integer commodityId) {
        return shoppingCartMapper.getPriceId(commodityId);
    }

    //根据commodityID获取imgId
    private String getImgId(Integer commodity) {
        return shoppingCartMapper.getImgId(commodity);
    }
}
